package net.wanho.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.mapper.MenuMapper;
import net.wanho.shop.mapper.UserMapper;
import net.wanho.shop.entity.Menu;
import net.wanho.shop.entity.User;
import net.wanho.shop.exception.LoginErrorException;
import net.wanho.shop.service.UserService;
import net.wanho.shop.vo.Page;
import net.wanho.shop.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author：汤小洋
 * Date：2025-09-16 15:52
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;
    private final MenuMapper menuMapper;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
    @Override
    public void register(User user) {
        user.setCreateTime(new Date());
        user.setRole("ROLE_USER");
        userMapper.insert(user);

        int i = 5/0;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS,readOnly = true, timeout = 5)
    @Override
    public UserVo login(User user) {
        /**
         * 判断账号密码是否正确
         */
        User u = userMapper.selectByUserNameAndPassword(user.getUserName(), user.getPassword());
        if (u == null){
            throw new LoginErrorException("用户名或密码不正确");
        }

        /**
         * 封装数据
         */
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(u, userVo);

        // 获取用户菜单
        String roleFlag = u.getRole();
        List<Menu> menus = menuMapper.selectByRoleFlag(roleFlag);

        // 获取一级菜单
        List<Menu> list = menus.stream()
                .filter(item -> item.getPid() == null || item.getPid() == 0)
                .map(item -> {
                    // 获取二级菜单
                    List<Menu> children = menus.stream()
                            .filter(subItem -> item.getId().equals(subItem.getPid()))
                            .collect(Collectors.toList());
                    item.setChildren(children);
                    return item;
                })
                .collect(Collectors.toList());

        userVo.setMenus(list);

        return userVo;
    }

    @Override
    public User getUserByUserName(String userName) {
        return userMapper.selectByUserName(userName);
    }

    @Override
    public Page<User> findByPage(Integer pageNum, Integer pageSize, User user) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectParams(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return new Page<>(pageInfo.getTotal(), pageInfo.getList());
    }
}
